package palms.messages;

import java.io.IOException;
import java.net.Socket;

import javax.crypto.SealedObject;

import palms.exceptions.InvalidClassMessage;
import palms.exceptions.InvalidTicketMessage;
import palms.exceptions.InvalidUserMessage;
import palms.security.PalmsSecurityManager;

public class SIRequest extends PMessage{

	private static final long serialVersionUID = -7140006095046677029L;

	private SealedObject siticket;
	private SealedObject serviceinfo;
	
	public SIRequest(SealedObject siticket, SealedObject serviceinfo) {
		this.siticket = siticket;
		this.serviceinfo = serviceinfo;
	}
	public SIRequest() {
	}
	public SealedObject getSiticket() {
		return siticket;
	}
	public void setSiticket(SealedObject siticket) {
		this.siticket = siticket;
	}
	public SealedObject getServiceinfo() {
		return serviceinfo;
	}
	public void setServiceinfo(SealedObject serviceinfo) {
		this.serviceinfo = serviceinfo;
	}
	
	public void sendTroughSocket(Socket s) throws IOException{
		PalmsSecurityManager.sendObjectTroughtSocket(s, new SIRequest());
		PalmsSecurityManager.sendObjectTroughtSocket(s, this.getSiticket());
		PalmsSecurityManager.sendObjectTroughtSocket(s, this.getServiceinfo());
	}
	
	public static SIRequest receiveTroughSocket(Socket s) throws IOException, ClassNotFoundException, InvalidClassMessage, InvalidUserMessage, InvalidTicketMessage{
		Object o = PalmsSecurityManager.receiveObjectTroughSocket(s);
		if(!(o instanceof SIRequest))
			PalmsSecurityManager.throwCommException(o);
		else{
			SIRequest sr = (SIRequest)o;
			SealedObject ticket = (SealedObject) PalmsSecurityManager.receiveObjectTroughSocket(s);
			SealedObject sinfo = (SealedObject) PalmsSecurityManager.receiveObjectTroughSocket(s);
			sr.setSiticket(ticket);
			sr.setServiceinfo(sinfo);
			return sr;
		}
		return null;
	}
}
